package edu.lnu.project.timeeditapp.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.lnu.project.timeeditapp.R;
import edu.lnu.project.timeeditapp.helper.MyHelper;


@SuppressWarnings({"deprecation"})
public class VisibilityColorAdapter extends SimpleAdapter {


	private Context mContext;
	int checkedColor;

	int count = 0;
	
	public SharedPreferences visibilityColorPreferences;
	public  SharedPreferences.Editor visibilityColorPreferencesEditor;
	
	public VisibilityColorAdapter(Context context,
			ArrayList<HashMap<String, String>> data, int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		mContext = context;
		initializeSharedPreferences(mContext);

	}
	
	public void initializeSharedPreferences(Context mContext){
		visibilityColorPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
		visibilityColorPreferencesEditor = visibilityColorPreferences.edit();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View mView = super.getView(position, convertView, parent);

		final TextView courseName = (TextView) mView.findViewById(R.id.courseName);
		final ImageButton visibilityButton = (ImageButton)mView.findViewById(R.id.visibilityButton);
		final Button colorButton = (Button) mView.findViewById(R.id.colorButton);

		visibilityButton.setTag(courseName.getText()+
				MyHelper.VISIBILITY);
		colorButton.setTag(courseName.getText()+
				MyHelper.COLOR);

		final Resources res = mContext.getResources();

		boolean visibility = visibilityColorPreferences.getBoolean(courseName.getText()+
				MyHelper.VISIBILITY, true);
		if(visibility){
			visibilityButton.setBackgroundDrawable(res.getDrawable(R.drawable.visible));
		}
		else{
			visibilityButton.setBackgroundDrawable(res.getDrawable(R.drawable.invisible));
		}

		visibilityButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String key = v.getTag()+"";
				boolean visibility = visibilityColorPreferences.getBoolean(key,
						true);
				if(visibility){
					visibilityButton.setBackgroundDrawable(res.getDrawable(R.drawable.invisible));
					visibilityColorPreferencesEditor.putBoolean(key, false).commit();
				}
				else{
					visibilityButton.setBackgroundDrawable(res.getDrawable(R.drawable.visible));
					visibilityColorPreferencesEditor.putBoolean(key, true).commit();
				}
			}
		});

		colorButton.setBackgroundColor(res.getColor(visibilityColorPreferences.getInt(courseName.getText()+
				MyHelper.COLOR, MyHelper.toInt(colorButton.getText()+""))));
		colorButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				/*Initialize the color selection dialog Views ******** */
				final String key = v.getTag()+"";

				LinearLayout lin1 = new LinearLayout(mContext);
				lin1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				lin1.setOrientation(LinearLayout.VERTICAL);

				HorizontalScrollView sv = new HorizontalScrollView(mContext);
				LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, 150);
				sv.setLayoutParams(lp);

				TextView tv = new TextView(mContext);
				tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				tv.setText("Scroll for more color options---->");

				RadioGroup rg1 = new RadioGroup(mContext);
				lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
				rg1.setLayoutParams(lp);
				rg1.setOrientation(RadioGroup.HORIZONTAL);

				final Resources res = mContext.getResources();
				int i = 0;
				for (int color : MyHelper.colors) {
					if(i == 14){
						break;
					}
					RadioButton rb = new RadioButton(mContext);
					rb.setLayoutParams(new LayoutParams(90, 90));
					rb.setBackgroundColor(res.getColor(color));
					rb.setTag(i);
					rb.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							checkedColor = MyHelper.toInt(v.getTag());
						}
					});
					rg1.addView(rb);
					i++;

				}
				lin1.addView(rg1);
				lin1.addView(tv);

				sv.addView(lin1);
				/*end initializing the views in the dialog**********/
				
				new AlertDialog.Builder(mContext)
				.setTitle("Please select a color...").setView(sv)
				.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						visibilityColorPreferencesEditor.putInt(key, MyHelper.colors.get(checkedColor)).commit();
						colorButton.setBackgroundColor(res.getColor(MyHelper.colors.get(checkedColor)));
					}
				})
				.setNegativeButton(android.R.string.cancel, null).show();
			}
		});
		colorButton.setText("");
		return mView;
	}
}
